/**
*      author: Y_Yao
*      created: 2025-04-15 15:04:21
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 8e4 + 7;
int n,m,u,v,w,b;
int s[N],dfn[N],low[N],dw[N],dp[N];
float h;
bool vis[N];
int step;

struct node
{
    int v,w;
    float h;
};

stack<int> st;
vector<node> E[N];

int tarjan(int x)
{
    if(vis[x]) return dfn[x];
    if(dfn[x]) return INF;

    dfn[x] = low[x] = step++;
    st.push(x);
    vis[x] = true;
    for(int i = 0;i < E[x].size();i++)
    {
        low[x] = min(low[x],tarjan(E[x][i].v));
    }
    if(dfn[x] == low[x])
    {
        while(!st.empty())
        {
            v = st.top(),st.pop();
            s[v] = x;
            vis[v] = false;
            if(v == x) break;
        }
    }
    return low[x];
}

void spfa(int x)
{
    queue<int> q;
    q.push(x);
    vis[s[x]] = 1;
    while(!q.empty())
    {
        u = q.front(),q.pop();
        vis[s[u]] = 0;

        for(int i = 0;i < E[s[u]].size();i++)
        {
            v = E[s[u]][i].v,w = E[s[u]][i].w + dp[s[u]],h = E[s[u]][i].h;
            if(s[u] == s[v] || w + dw[s[v]] <= dp[s[v]]) continue;
            dp[s[v]] = w + dw[s[v]];
            if(!vis[s[v]])
            {
                vis[s[v]] = 1;
                q.push(s[v]);
            } 
        }
    }
}

void solve()
{
    cin >> n >> m;
    for(int i = 1;i <= m;i++)
    {
        cin >> u >> v >> w >> h;
        E[u].push_back({v,w,h});
    }

    for(int i = 1;i <= n;i++)
    {
        s[i] = i;
    }
    cin >> b;
    tarjan(b);
    for(int i = 1;i <= n;i++)
    {
        for(int j = 0;j < E[i].size();j++)
        {
            v = E[i][j].v,w = E[i][j].w,h = E[i][j].h;
            if(s[i] != s[v]) continue;
            while(w)
            {
                dw[s[i]] += w;
                w *= h;
            }
        }
    }

    for(int i = 1;i <= n;i++)
    {
        for(int j = 0;j < E[i].size();j++)
        {
            if(s[i] == i) break;
            E[s[i]].push_back(E[i][j]);
        }
    }
    dp[b] = dw[s[b]];
    spfa(s[b]);
    int ans = 0;
    for(int i = 1;i <= n;i++)
    {
        ans = max(ans,dp[i]);
    }
    cout << ans << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    // cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}